地圖概觀:
* x 軸是經度 ( Longitude ) ,y 軸是緯度 ( Latitude )
# install.packages("ggmap")
# 如果安裝過程中遇到無法安裝png, jpeg 等問題 (macOS)
# 請在terminal 執行 brew link libpng 和 brew link libjpeg,重試安裝
library(ggmap)
## Loading required package: ggplot2
library(mapproj)
## Loading required package: maps
map <- get_map(location = '台北市', zoom = 11, maptype = "roadmap")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=%E5%8F%B0%E5%8C%97%E5%B8%82&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=%E5%8F%B0%E5%8C%97%E5%B8%82&sensor=false
ggmap(map)
下面的範例利用前面練習 Data Visualization 的資料進行延伸,
library(maps)
# us.cities 包含 us 所有的country name, etc, population, lat, lon, capital
# 因為 txhousing 沒有經緯度資訊,所要利用 us.cities 找出 lat and lon
data(us.cities)
# Preprocessing
# collect all the city name in txhousing
mycountry <- unique(txhousing$city)
# beacause the us.cities$name is end in "TX", so we need to cut it off
tx.cities <- subset(us.cities, country.etc == "TX")
tx.cities$city <- unlist(strsplit(tx.cities$name, " TX"))
# for use rep to map the lat and long data conveniently, we choose only one year
m.txhousing <- subset(txhousing, year == 2000 & city %in% tx.cities$city)
# there are 12 months data in 2000, so set each = 12
temp <- tx.cities[tx.cities$city %in% m.txhousing$city, c("lat", "long")]
temp <- temp[rep(seq_len(nrow(temp)), each = 12), ]
m.txhousing.geo <- cbind(m.txhousing, temp)
# print data
head(m.txhousing.geo)
## city year month sales volume median listings inventory date
## 1 Abilene 2000 1 72 5380000 71400 701 6.3 2000.000
## 1.1 Abilene 2000 2 98 6505000 58700 746 6.6 2000.083
## 1.2 Abilene 2000 3 130 9285000 58100 784 6.8 2000.167
## 1.3 Abilene 2000 4 98 9730000 68600 785 6.9 2000.250
## 1.4 Abilene 2000 5 141 10590000 67300 794 6.8 2000.333
## 1.5 Abilene 2000 6 156 13910000 66900 780 6.6 2000.417
## lat long
## 1 32.45 -99.74
## 1.1 32.45 -99.74
## 1.2 32.45 -99.74
## 1.3 32.45 -99.74
## 1.4 32.45 -99.74
## 1.5 32.45 -99.74
# draw plot
ggplot(m.txhousing.geo, aes(x = long, y = lat, size = sales, colour = cut(median, 5))) + # 設定ggplot圖資
borders("county", "texas", colour = "grey70") + # 設定圖在德州,用county區分
geom_point(alpha=.5) + # 顯示圖上的資料點
facet_wrap(~month) + # 區分出不同 month 的資料
ggtitle("Housing market for populous cities in Texas (2000)") + # set plot title
scale_colour_discrete(name = "Median price") + # 設定不同顏色資料點的意義
scale_size_continuous(name = "Number of Sales") # 設定不同大小資料的意義
## Warning: Removed 37 rows containing missing values (geom_point).
以下練習用 ggmap 結合 txhousing 的資料
tx_center = as.numeric(geocode("Texas"))
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Texas&sensor=false
# 設定 ggmap
txMap = ggmap(get_googlemap(center=tx_center, scale=2, zoom=6), extent="normal")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=31.968599,-99.901813&zoom=6&size=640x640&scale=2&maptype=terrain&sensor=false
tx.cities.all <- subset(us.cities, country.etc == "TX")
# ggmapp 和 ggplot2 結合使用,可以在實體地圖上顯示資料
txMap + geom_point(aes(x=long, y=lat, size = pop), col = "orange",
data = tx.cities.all, alpha=0.4) +
ggtitle("Population of Texas cities")